CMAKE_MINIMUM_REQUIRED(VERSION 3.6.3) # OPENSSL_USE_STATIC_LIBS
###################### Branding ##########################

SET(BRANDING_PROJECT_NAME "FastoNoSQL" CACHE STRING "Branding for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_VERSION "2.6.0.1" CACHE STRING "Branding version for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_BUILD_TYPE_VERSION "release" CACHE STRING "Build version type for ${BRANDING_PROJECT_NAME}") #default
  # possible variables: alfa, beta, rc, release
SET(BRANDING_PROJECT_TYPE_VERSION "community" CACHE STRING "Build version type for ${BRANDING_PROJECT_NAME}") #default
  # possible variables: community, pro, enterprise
  # for pro and enterprise BUILD_TYPE can be (community, public, private)
  # in pricate need to specify First/Last names

SET(BRANDING_PROJECT_DOMAIN "https://fastonosql.com" CACHE STRING "Branding domain url for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_DOWNLOAD_LINK "https://fastonosql.com/anonim_users_downloads" CACHE STRING "Branding download root url for ${BRANDING_PROJECT_NAME}")
SET(BRANDING_PROJECT_DOWNLOAD_LINK_PRO "https://fastonosql.com/pro_users_downloads" CACHE STRING "Branding download root url for ${BRANDING_PROJECT_NAME}")
SET(BRANDING_PROJECT_DOWNLOAD_LINK_ENTERPRISE "https://fastonosql.com/enterprise_users_downloads" CACHE STRING "Branding download root url for ${BRANDING_PROJECT_NAME}")
SET(BRANDING_PROJECT_COMPANYNAME "FastoGT" CACHE STRING "Company name for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_COMPANYNAME_DOMAIN "http://fastogt.com" CACHE STRING "Internet domain name company for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_MAINTAINER_MAIL "support@fastogt.com" CACHE STRING "Internet mail address mainteiner of ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_MAINTAINER_NAME "Alexandr Topilski" CACHE STRING "Name of mainteiner for ${BRANDING_PROJECT_NAME}") #default

# social links
SET(BRANDING_PROJECT_TWITTER_URL "https://twitter.com/fastonosql" CACHE STRING "Branding twitter url for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_FACEBOOK_URL "https://facebook.com/profile.php?id=100004938436506" CACHE STRING "Branding facebook url for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_INSTAGRAM_URL "https://instagram.com/fastoredis" CACHE STRING "Branding instagram url for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_YOUTUBE_URL "https://youtube.com/channel/UCdUnh74gr6-HmKa8S6C3viA" CACHE STRING "Branding youtube url for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_HOW_TO_USE_URL "https://github.com/fastogt/fastonosql/wiki" CACHE STRING "Branding howto url for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_EMAIL_URL "mailto:support@fastogt.com" CACHE STRING "Branding support email url for ${BRANDING_PROJECT_NAME}") #default

SET(BRANDING_PROJECT_GITHUB_URL "https://github.com/fastogt/fastonosql" CACHE STRING "Branding github url for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_GITHUB_ISSUES "https://github.com/fastogt/fastonosql/issues" CACHE STRING "Branding issues url for ${BRANDING_PROJECT_NAME}") #default

SET(BRANDING_PROJECT_SUMMARY "Cross-platform open source Redis, Memcached, SSDB, LevelDB, RocksDB, UnQLite, LMDB, Pika, Dynomite, KeyDB GUI management tool."
  CACHE STRING "Short description of ${BRANDING_PROJECT_NAME}"
)
SET(BRANDING_PROJECT_DESCRIPTION "${BRANDING_PROJECT_NAME} it is GUI platform for NoSQL databases. Currently we support next databases: \
  * Redis \
  * Memcached \
  * SSDB \
  * LevelDB \
  * RocksDB \
  * UnQLite \
  * LMDB \
  * Pika \
  * Dynomite \
  * KeyDB \
  For all this databases you can connect and use them in any way you need. \
  Also ${BRANDING_PROJECT_NAME} is IDE tool for NoSQL which works on the most famous platforms:\
  Windows, Linux, MacOSX, Android, FreeBSD."
  CACHE STRING "Description of ${BRANDING_PROJECT_NAME}")
SET(BRANDING_PROJECT_COPYRIGHT "Copyright (C) 2014-2020 ${BRANDING_PROJECT_COMPANYNAME} All Rights Reserved."
  CACHE STRING "Copyright notice for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_CHANGELOG_FILE CHANGELOG
  CACHE STRING "Branding for changelog file ${BRANDING_PROJECT_NAME}
  (File name given as relative paths are interpreted with respect to the src source directory)") #default
SET(BRANDING_PROJECT_LICENSE_FILE_NAME LICENSE
  CACHE STRING "Branding for license file ${BRANDING_PROJECT_NAME}
  (File name given as relative paths are interpreted with respect to the src source directory)") #default

PROJECT(${BRANDING_PROJECT_NAME} VERSION ${BRANDING_PROJECT_VERSION} LANGUAGES CXX C)
SET(CMAKE_CXX_STANDARD 14)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_EXTENSIONS OFF)
SET(CMAKE_C_STANDARD 99)
SET(CMAKE_C_STANDARD_REQUIRED ON)
#SET(CMAKE_C_EXTENSIONS OFF)
#################### Project Settings ####################
SET(PROJECT_TYPE_VERSION ${BRANDING_PROJECT_TYPE_VERSION})
IF(PROJECT_TYPE_VERSION STREQUAL "enterprise")
  SET(PROJECT_NAME_TITLE "${PROJECT_NAME} ENTERPRISE") #PROJECT_NAME in cache
  SET(PROJECT_DOWNLOAD_LINK ${BRANDING_PROJECT_DOWNLOAD_LINK_ENTERPRISE})
  SET(ENTERPRISE_VERSION INTERNAL ON)
ELSEIF(PROJECT_TYPE_VERSION STREQUAL "pro")
  SET(PROJECT_NAME_TITLE "${PROJECT_NAME} PRO") #PROJECT_NAME in cache
  SET(PROJECT_DOWNLOAD_LINK ${BRANDING_PROJECT_DOWNLOAD_LINK_PRO})
  SET(PRO_VERSION INTERNAL ON)
ELSEIF(PROJECT_TYPE_VERSION STREQUAL "community")
  SET(PROJECT_NAME_TITLE ${PROJECT_NAME}) #PROJECT_NAME in cache
  SET(PROJECT_DOWNLOAD_LINK ${BRANDING_PROJECT_DOWNLOAD_LINK})
  SET(COMMUNITY_VERSION INTERNAL ON)
ELSE()
  MESSAGE(FATAL_ERROR "Not supported build type version: ${PROJECT_TYPE_VERSION}")
ENDIF(PROJECT_TYPE_VERSION STREQUAL "enterprise")

SET(PROJECT_DOMAIN ${BRANDING_PROJECT_DOMAIN})
SET(PROJECT_COMPANYNAME ${BRANDING_PROJECT_COMPANYNAME})
SET(PROJECT_COPYRIGHT ${BRANDING_PROJECT_COPYRIGHT})
SET(PROJECT_SUMMARY ${BRANDING_PROJECT_SUMMARY})
SET(PROJECT_DESCRIPTION ${BRANDING_PROJECT_DESCRIPTION})
SET(PROJECT_COMPANYNAME_DOMAIN ${BRANDING_PROJECT_COMPANYNAME_DOMAIN})
SET(PROJECT_MAINTAINER_MAIL ${BRANDING_PROJECT_MAINTAINER_MAIL})
SET(PROJECT_MAINTAINER_NAME ${BRANDING_PROJECT_MAINTAINER_NAME})
SET(PROJECT_TWITTER_URL ${BRANDING_PROJECT_TWITTER_URL})
SET(PROJECT_FACEBOOK_URL ${BRANDING_PROJECT_FACEBOOK_URL})
SET(PROJECT_INSTAGRAM_URL ${BRANDING_PROJECT_INSTAGRAM_URL})
SET(PROJECT_YOUTUBE_URL ${BRANDING_PROJECT_YOUTUBE_URL})
SET(PROJECT_HOW_TO_USE_URL ${BRANDING_PROJECT_HOW_TO_USE_URL})
SET(PROJECT_EMAIL_URL ${BRANDING_PROJECT_EMAIL_URL})
SET(PROJECT_GITHUB_URL ${BRANDING_PROJECT_GITHUB_URL})
SET(PROJECT_GITHUB_ISSUES ${BRANDING_PROJECT_GITHUB_ISSUES})
SET(PROJECT_LICENSE_FILE_NAME ${BRANDING_PROJECT_LICENSE_FILE_NAME})

SET(PROJECT_CHANGELOG_FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/CHANGELOG)
SET(DATE_CMD "date")
SET(DATE_ARGS "+%a %b %d %Y")
EXECUTE_PROCESS(COMMAND ${DATE_CMD} ${DATE_ARGS} RESULT_VARIABLE CHANGELOG_TIMESTAMP_RESULT OUTPUT_VARIABLE CHANGELOG_TIMESTAMP_OUTPUT)  #for rpm package
IF (NOT "${CHANGELOG_TIMESTAMP_OUTPUT}" STREQUAL "")
  STRING(REPLACE "\n" "" CHANGELOG_TIMESTAMP_OUTPUT ${CHANGELOG_TIMESTAMP_OUTPUT})
ELSE()
  MESSAGE(WARNING "Failed to get timestamp: ${CHANGELOG_TIMESTAMP_RESULT}")
ENDIF(NOT "${CHANGELOG_TIMESTAMP_OUTPUT}" STREQUAL "")
FILE(WRITE ${PROJECT_CHANGELOG_FILE} "* ${CHANGELOG_TIMESTAMP_OUTPUT} ${PROJECT_MAINTAINER_NAME} <${PROJECT_MAINTAINER_MAIL}>\n")
FILE(READ ${BRANDING_PROJECT_CHANGELOG_FILE} CHANGELOG_TEXT)
FILE(APPEND ${PROJECT_CHANGELOG_FILE} ${CHANGELOG_TEXT})

SET(PROJECT_BUILD_TYPE_VERSION ${BRANDING_PROJECT_BUILD_TYPE_VERSION})
##########################################################

STRING(TOLOWER ${PROJECT_NAME} PROJECT_NAME_LOWERCASE)
STRING(TOUPPER ${PROJECT_NAME} PROJECT_NAME_UPPERRCASE)

SET(PROJECT_VERSION_SHORT ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}) #PROJECT_VERSION_* in cache
SET(PROJECT_VERSION_INTEGER ${PROJECT_VERSION_MAJOR}${PROJECT_VERSION_MINOR}${PROJECT_VERSION_PATCH}) #PROJECT_VERSION_* in cache

MESSAGE(STATUS "PROJECT_VERSION: ${PROJECT_VERSION}")

IF(APPLE AND CMAKE_OSX_SYSROOT)
  STRING(REGEX REPLACE ".*MacOSX([0-9]+)\\.([0-9]+).*$" "\\1" DARWIN_MAJOR_SDK_VERSION "${CMAKE_OSX_SYSROOT}")
  STRING(REGEX REPLACE ".*MacOSX([0-9]+)\\.([0-9]+).*$" "\\2" DARWIN_MINOR_SDK_VERSION "${CMAKE_OSX_SYSROOT}")
  IF(EXISTS "${CMAKE_OSX_SYSROOT}")
    SET(MACOSX_DEPLOYMENT_TARGET ${DARWIN_MAJOR_SDK_VERSION}.${DARWIN_MINOR_SDK_VERSION})
    SET(CMAKE_CXX_FLAGS "--sysroot ${CMAKE_OSX_SYSROOT} ${CMAKE_CXX_FLAGS}")
    MESSAGE(STATUS "Setting MACOSX_DEPLOYMENT_TARGET to '${MACOSX_DEPLOYMENT_TARGET}'.")
  ENDIF()
ENDIF(APPLE AND CMAKE_OSX_SYSROOT)

########################## Options #########################
IF(MSVC)
  IF(CMAKE_CL_64)
    OPTION(BUILD_64X "Build 64bit project" ON)
  ELSE()
    OPTION(BUILD_64X "Build 64bit project" OFF)
  ENDIF(CMAKE_CL_64)
ELSE(MSVC)
  IF(CMAKE_SIZEOF_VOID_P MATCHES 8)
    OPTION(BUILD_64X "Build 64bit project" ON)
  ELSE(CMAKE_SIZEOF_VOID_P MATCHES 8)
    OPTION(BUILD_64X "Build 64bit project" OFF)
  ENDIF(CMAKE_SIZEOF_VOID_P MATCHES 8)
ENDIF(MSVC)

OPTION(BUILD_WITH_REDIS "Build with Redis support" ON)
OPTION(BUILD_WITH_PIKA "Build with Pika support" OFF)
OPTION(BUILD_WITH_DYNOMITE "Build with Dynomite support" OFF)
OPTION(BUILD_WITH_KEYDB "Build with KeyDB support" ON)
OPTION(BUILD_WITH_MEMCACHED "Build with Memcached support" ON)
OPTION(BUILD_WITH_SSDB "Build with SSDB support" ON)
OPTION(BUILD_WITH_LEVELDB "Build with LevelDB support" ON)
OPTION(BUILD_WITH_ROCKSDB "Build with RocksDB support" ON) #for android off
OPTION(BUILD_WITH_UNQLITE "Build with Unqlite support" ON)
OPTION(BUILD_WITH_LMDB "Build with LMDB support" ON)

OPTION(LOG_TO_FILE "Logging to file" OFF)
OPTION(GENERATE_TRANSLATIONS "Generate ts-files for ${PROJECT_NAME}" OFF)
OPTION(QT_ENABLED "Enable Qt support" ON)
OPTION(CPACK_SUPPORT "Enable package support" ON)
OPTION(DEVELOPER_ENABLE_TESTS "Enable tests for ${PROJECT_NAME_TITLE} project" OFF)
OPTION(DEVELOPER_CHECK_STYLE "Enable check style for ${PROJECT_NAME_TITLE} project" OFF)
OPTION(DEVELOPER_GENERATE_DOCS "Generate docs api for ${PROJECT_NAME_TITLE} project" OFF)

MESSAGE(STATUS "BUILD_64X: ${BUILD_64X}")
MESSAGE(STATUS "PROJECT_TYPE_VERSION: ${PROJECT_TYPE_VERSION}")
MESSAGE(STATUS "BUILD_WITH_REDIS: ${BUILD_WITH_REDIS}")
MESSAGE(STATUS "BUILD_WITH_PIKA: ${BUILD_WITH_PIKA}")
MESSAGE(STATUS "BUILD_WITH_DYNOMITE: ${BUILD_WITH_DYNOMITE}")
MESSAGE(STATUS "BUILD_WITH_KEYDB: ${BUILD_WITH_KEYDB}")
MESSAGE(STATUS "BUILD_WITH_MEMCACHED: ${BUILD_WITH_MEMCACHED}")
MESSAGE(STATUS "BUILD_WITH_SSDB: ${BUILD_WITH_SSDB}")
MESSAGE(STATUS "BUILD_WITH_LEVELDB: ${BUILD_WITH_LEVELDB}")
MESSAGE(STATUS "BUILD_WITH_ROCKSDB: ${BUILD_WITH_ROCKSDB}")
MESSAGE(STATUS "BUILD_WITH_UNQLITE: ${BUILD_WITH_UNQLITE}")
MESSAGE(STATUS "BUILD_WITH_LMDB: ${BUILD_WITH_LMDB}")

##################################DEFAULT VALUES##########################################
# Set the build type.  Options are:
#  Debug          : w/ debug symbols, w/o optimization
#  Release        : w/o debug symbols, w/ optimization
#  RelWithDebInfo : w/ debug symbols, w/ optimization
#  MinSizeRel     : w/o debug symbols, w/ optimization, stripped binaries
IF(NOT CMAKE_BUILD_TYPE)
  SET(CMAKE_BUILD_TYPE DEBUG)
ENDIF(NOT CMAKE_BUILD_TYPE)

# If the user did not customize the install prefix,
# set it to live under build so we don't inadvertently pollute /usr/local
IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  SET(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "default install path" FORCE)
ENDIF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)

IF("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}")
  MESSAGE(SEND_ERROR "In-source builds are not allowed.")
ENDIF("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}")

MESSAGE(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")

############################################################################

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/cmake")
INCLUDE(config) ###################
DEFINE_DEFAULT_DEFINITIONS(ON ON OFF)
DEFINE_PROJECT_DEFINITIONS()

# Search libraries
# use Qt
DETECT_QT()
##################### Libraries search ###################
SET(QT_COMPONENTS_TO_USE Qt5Core Qt5Gui Qt5Widgets)
IF(OS_MACOSX)
  SET(QT_COMPONENTS_TO_USE ${QT_COMPONENTS_TO_USE} Qt5MacExtras)
ELSEIF(OS_LINUX)
  SET(QT_COMPONENTS_TO_USE ${QT_COMPONENTS_TO_USE} Qt5DBus) #for libqxcb
ENDIF(OS_MACOSX)

INTEGRATE_QT(${QT_COMPONENTS_TO_USE})
##########################################################

IF(OS_ANDROID)
  SET(ARCH_TEXT_PACKAGE ${ANDROID_NDK_ABI_NAME})
ELSE()
  IF(BUILD_64X)
    SET_PROPERTY(GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS 1)
    IF(OS_MACOSX)
      SET(CMAKE_OSX_ARCHITECTURES x86_64)
    ENDIF(OS_MACOSX)
    SET(ARCH_TEXT_PACKAGE x86_64)#amd64
  ELSE()
    SET_PROPERTY(GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS 0)
    IF(OS_MACOSX)
      SET(CMAKE_OSX_ARCHITECTURES i386)
    ENDIF(OS_MACOSX)
    SET(ARCH_TEXT_PACKAGE i386)
  ENDIF(BUILD_64X)
ENDIF(OS_ANDROID)

IF(OS_WINDOWS)
  SET(RESOURCE_OS     "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/win.rc")
  SET(RESOURCE_OS_IN  "${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/windows/winres.rc.in")
  SET(ICON_FILE_IN    "${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/windows/icon.ico")
  SET(LIB_INSTALL_DESTINATION .)
  SET(TARGET_INSTALL_DESTINATION .)
  SET(SHARE_INSTALL_DESTINATION share)
  SET(RELATIVE_SOURCE_DIR .)
  SET(EXECUTABLE_NAME ${PROJECT_NAME}.exe)
ELSEIF(OS_MACOSX)
  SET(BUNDLE_BASE_NAME ${PROJECT_NAME})
  SET(BUNDLE_NAME ${BUNDLE_BASE_NAME}.app)
  SET(RESOURCE_OS     "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME_LOWERCASE}.desktop")
  SET(RESOURCE_OS_IN  "${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/desktop.in")
  SET(ICON_FILE_IN    "${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/osx/icon.icns")
  SET(LIB_INSTALL_DESTINATION .)
  SET(TARGET_INSTALL_DESTINATION .)
  SET(SHARE_INSTALL_DESTINATION ${BUNDLE_NAME}/Contents/share)
  SET(RELATIVE_SOURCE_DIR Contents)
  SET(EXECUTABLE_NAME ${BUNDLE_NAME})
ELSEIF(OS_LINUX)
  SET(RESOURCE_OS     "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME_LOWERCASE}.desktop")
  SET(RESOURCE_OS_IN  "${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/desktop.in")
  SET(ICON_FILE_IN    "${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/linux/icon.png")
  SET(LIB_INSTALL_DESTINATION lib)
  SET(TARGET_INSTALL_DESTINATION bin)
  SET(SHARE_INSTALL_DESTINATION share)
  SET(RELATIVE_SOURCE_DIR ..)
  SET(EXECUTABLE_NAME ${PROJECT_NAME})
ELSEIF(OS_FREEBSD)
  SET(RESOURCE_OS     "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME_LOWERCASE}.desktop")
  SET(RESOURCE_OS_IN  "${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/desktop.in")
  SET(ICON_FILE_IN    "${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/linux/icon.png")
  SET(LIB_INSTALL_DESTINATION lib)
  SET(TARGET_INSTALL_DESTINATION bin)
  SET(SHARE_INSTALL_DESTINATION share)
  SET(RELATIVE_SOURCE_DIR ..)
  SET(EXECUTABLE_NAME ${PROJECT_NAME})
ELSEIF(OS_ANDROID)
  SET(RESOURCE_OS     "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME_LOWERCASE}.desktop")
  SET(RESOURCE_OS_IN  "${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/desktop.in")
  SET(ICON_FILE_IN    "${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/android/icon.png")
  SET(LIB_INSTALL_DESTINATION libs/${ANDROID_NDK_ABI_NAME})
  SET(TARGET_INSTALL_DESTINATION libs/${ANDROID_NDK_ABI_NAME}) #libs/armeabi-v7a
  SET(SHARE_INSTALL_DESTINATION libs/${ANDROID_NDK_ABI_NAME}/share)
  SET(RELATIVE_SOURCE_DIR .)
  SET(EXECUTABLE_NAME ${PROJECT_NAME})
ENDIF(OS_WINDOWS)
ADD_DEFINITIONS(-DRELATIVE_SOURCE_DIR="${RELATIVE_SOURCE_DIR}")
GET_FILENAME_COMPONENT(ICON_FILE_IN_EXTENSION ${ICON_FILE_IN} EXT)
SET(ICON_FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME_LOWERCASE}${ICON_FILE_IN_EXTENSION})
CONFIGURE_FILE("${ICON_FILE_IN}" "${ICON_FILE}" COPYONLY)

# project sources
SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)

IF(ENTERPRISE_VERSION)
  ADD_DEFINITIONS(-DENTERPRISE_VERSION)
ELSEIF(PRO_VERSION)
  ADD_DEFINITIONS(-DPRO_VERSION)
ELSEIF(COMMUNITY_VERSION)
  ADD_DEFINITIONS(-DCOMMUNITY_VERSION)
ENDIF(ENTERPRISE_VERSION)
IF(BUILD_WITH_REDIS)
  ADD_DEFINITIONS(-DBUILD_WITH_REDIS)
ENDIF(BUILD_WITH_REDIS)
IF(BUILD_WITH_PIKA)
  ADD_DEFINITIONS(-DBUILD_WITH_PIKA)
ENDIF(BUILD_WITH_PIKA)
IF(BUILD_WITH_DYNOMITE)
  ADD_DEFINITIONS(-DBUILD_WITH_DYNOMITE)
ENDIF(BUILD_WITH_DYNOMITE)
IF(BUILD_WITH_KEYDB)
  ADD_DEFINITIONS(-DBUILD_WITH_KEYDB)
ENDIF(BUILD_WITH_KEYDB)
IF(BUILD_WITH_MEMCACHED)
  ADD_DEFINITIONS(-DBUILD_WITH_MEMCACHED)
ENDIF(BUILD_WITH_MEMCACHED)
IF(BUILD_WITH_SSDB)
  ADD_DEFINITIONS(-DBUILD_WITH_SSDB)
ENDIF(BUILD_WITH_SSDB)
IF(BUILD_WITH_LEVELDB)
  ADD_DEFINITIONS(-DBUILD_WITH_LEVELDB)
ENDIF(BUILD_WITH_LEVELDB)
IF(BUILD_WITH_ROCKSDB)
  ADD_DEFINITIONS(-DBUILD_WITH_ROCKSDB)
ENDIF(BUILD_WITH_ROCKSDB)
IF(BUILD_WITH_UNQLITE)
  ADD_DEFINITIONS(-DBUILD_WITH_UNQLITE)
ENDIF(BUILD_WITH_UNQLITE)
IF(BUILD_WITH_LMDB)
  ADD_DEFINITIONS(-DBUILD_WITH_LMDB)
ENDIF(BUILD_WITH_LMDB)

IF(LOG_TO_FILE)
  ADD_DEFINITIONS(-DLOG_TO_FILE)
ENDIF(LOG_TO_FILE)

ADD_DEFINITIONS(
  -DPROJECT_SUMMARY="${PROJECT_SUMMARY}"
  -DPROJECT_DESCRIPTION="${PROJECT_DESCRIPTION}"
  -DPROJECT_TWITTER_URL="${PROJECT_TWITTER_URL}"
  -DPROJECT_FACEBOOK_URL="${PROJECT_FACEBOOK_URL}"
  -DPROJECT_YOUTUBE_URL="${PROJECT_YOUTUBE_URL}"
  -DPROJECT_HOW_TO_USE_URL="${PROJECT_HOW_TO_USE_URL}"
  -DPROJECT_EMAIL_URL="${PROJECT_EMAIL_URL}"
  -DPROJECT_INSTAGRAM_URL="${PROJECT_INSTAGRAM_URL}"
  -DPROJECT_GITHUB_URL="${PROJECT_GITHUB_URL}"
  -DPROJECT_GITHUB_ISSUES="${PROJECT_GITHUB_ISSUES}"
  -DPROJECT_DOWNLOAD_LINK="${PROJECT_DOWNLOAD_LINK}"
  -DPROJECT_ARCH="${ARCH_TEXT_PACKAGE}"
)

SET(PROJECT_VERSION_HUMAN "${PROJECT_VERSION} Revision: ${PROJECT_VERSION_GIT}")
ADD_DEFINITIONS(-DPROJECT_VERSION_HUMAN="${PROJECT_VERSION_HUMAN}")
ADD_SUBDIRECTORY(src)

#CPACK
IF(CPACK_SUPPORT)
  SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY ${PROJECT_SUMMARY})
  SET(CPACK_PACKAGE_DESCRIPTION ${PROJECT_DESCRIPTION})
  # CPACK_DEBIAN_PACKAGE_DESCRIPTION CPACK_RPM_PACKAGE_SUMMARY
  SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/COPYRIGHT")#CPACK_RPM_PACKAGE_DESCRIPTION
  SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/${PROJECT_LICENSE_FILE_NAME}")
  SET(CPACK_RESOURCE_FILE_README "${CMAKE_SOURCE_DIR}/README.md")
  SET(CPACK_RESOURCE_FILE_WELCOME "${CMAKE_SOURCE_DIR}/README.md")

  SET(CPACK_PACKAGE_VENDOR "${PROJECT_COMPANYNAME}")#CPACK_RPM_PACKAGE_VENDOR
  SET(CPACK_PACKAGE_CONTACT "${PROJECT_MAINTAINER_NAME} <${PROJECT_MAINTAINER_MAIL}>")#CPACK_DEBIAN_PACKAGE_MAINTAINER
    
  SET(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
  SET(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
  SET(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_SHORT})

  IF(NOT PROJECT_BUILD_TYPE_VERSION STREQUAL "release")
    SET(CPACK_PACKAGE_VERSION_PATCH "${CPACK_PACKAGE_VERSION_PATCH}-${PROJECT_BUILD_TYPE_VERSION}${PROJECT_VERSION_TWEAK}")
  ENDIF(NOT PROJECT_BUILD_TYPE_VERSION STREQUAL "release")

  SET(CPACK_PACKAGE_VERSION ${CPACK_PACKAGE_VERSION_PATCH})#CPACK_DEBIAN_PACKAGE_VERSION CPACK_RPM_PACKAGE_VERSION
  SET(CPACK_PACKAGE_NAME ${PROJECT_NAME_LOWERCASE})#CPACK_DEBIAN_PACKAGE_NAME CPACK_RPM_PACKAGE_NAME
IF(ENTERPRISE_VERSION)
  SET(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}_enterprise-${CPACK_PACKAGE_VERSION}-${ARCH_TEXT_PACKAGE}-${PROJECT_VERSION_GIT}")#out package name
ELSEIF(PRO_VERSION)
  SET(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}_pro-${CPACK_PACKAGE_VERSION}-${ARCH_TEXT_PACKAGE}-${PROJECT_VERSION_GIT}")#out package name
ELSE()
  SET(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${ARCH_TEXT_PACKAGE}-${PROJECT_VERSION_GIT}")#out package name
ENDIF(ENTERPRISE_VERSION)
  SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}")
  SET(CPACK_PACKAGE_INSTALL_DIRECTORY ${PROJECT_NAME})
  SET(CPACK_PACKAGE_EXECUTABLES "${PROJECT_NAME};${PROJECT_NAME}")
  MESSAGE(STATUS "CPACK_PACKAGE_FILE_NAME: ${CPACK_PACKAGE_FILE_NAME}")
  SET(CPACK_MONOLITHIC_INSTALL ON)

  IF(OS_WINDOWS)
    #NSIS
    IF(BUILD_64X)
      SET(CPACK_NSIS_INSTALL_ROOT "$PROGRAMFILES64")
    ELSE()
      SET(CPACK_NSIS_INSTALL_ROOT "$PROGRAMFILES")
    ENDIF(BUILD_64X)
    # There is a bug in NSI that does not handle full unix paths properly. Make
    # sure there is at least one set of four (4) backlasshes.
    # SET(CPACK_NSIS_MODIFY_PATH ON)
    SET(CPACK_PACKAGE_ICON "${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/windows\\\\nsis-top-logo.bmp")
    SET(CPACK_NSIS_INSTALLED_ICON_NAME "\\\\${PROJECT_NAME}.exe")
    SET(CPACK_CREATE_DESKTOP_LINKS "${PROJECT_NAME}.exe")
    SET(CPACK_NSIS_CREATE_ICONS "CreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\${PROJECT_NAME}.lnk\\\" \\\"$INSTDIR\\\\${PROJECT_NAME}.exe\\\"")
    SET(CPACK_NSIS_CREATE_ICONS_EXTRA "CreateShortCut  \\\"$DESKTOP\\\\${PROJECT_NAME}.lnk\\\" \\\"$INSTDIR\\\\${PROJECT_NAME}.exe\\\"")
    SET(CPACK_NSIS_DELETE_ICONS_EXTRA "Delete           \\\"$DESKTOP\\\\${PROJECT_NAME}.lnk\\\"")
    SET(CPACK_NSIS_DISPLAY_NAME "${CPACK_PACKAGE_INSTALL_DIRECTORY}")
    SET(CPACK_NSIS_HELP_LINK "${PROJECT_COMPANYNAME_DOMAIN}")
    SET(CPACK_NSIS_URL_INFO_ABOUT "${PROJECT_DOMAIN}")
    SET(SIDEBAR_IMAGE ${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/windows\\\\database.bmp)
    SET(CPACK_NSIS_INSTALLER_MUI_ICON_CODE "!define MUI_WELCOMEFINISHPAGE_BITMAP \\\"${SIDEBAR_IMAGE}\\\"")
    SET(CPACK_NSIS_CONTACT ${PROJECT_MAINTAINER_MAIL})
    SET(CPACK_NSIS_MUI_FINISHPAGE_RUN "..\\\\${PROJECT_NAME}.exe")
  ELSEIF(OS_MACOSX)
    # SET(CPACK_OSX_PACKAGE_VERSION "10.5")
  ELSEIF(OS_LINUX)
    SET(CPACK_STRIP_FILES ON)
    #SET(UBUNTU_LP_BUG 300472)
    #SET(CPACK_STRIP_FILES "bin/${PROJECT_NAME}")
    #SET(CPACK_SOURCE_STRIP_FILES "")

    SET(CPACK_PACKAGING_INSTALL_PREFIX "/opt/${PROJECT_NAME_LOWERCASE}")
    CONFIGURE_FILE("${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/linux/postinst.in" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/scripts/postinst" @ONLY IMMEDIATE)
    FILE(COPY ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/scripts/postinst DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}
      FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
    CONFIGURE_FILE("${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/linux/prerm.in" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/scripts/prerm" @ONLY IMMEDIATE)
    FILE(COPY ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/scripts/prerm DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}
      FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)

    #RPM
    # CPACK_RPM_PACKAGE_ARCHITECTURE CPACK_RPM_PACKAGE_LICENSE CPACK_RPM_PACKAGE_DESCRIPTION CPACK_RPM_SPEC_INSTALL_POST
    # SET(CPACK_PACKAGE_RELEASE 1)
    SET(CPACK_RPM_PACKAGE_LICENSE "GPL v3")
    SET(CPACK_RPM_PACKAGE_AUTOREQPROV "no")
    SET(CPACK_RPM_PACKAGE_REQUIRES "libc.so.6 >= 2.12, libstdc++.so.6 >= 6.0.13")
    SET(CPACK_RPM_PACKAGE_RELEASE 1)
    SET(CPACK_RPM_PACKAGE_GROUP "Development/database")
    SET(CPACK_RPM_PACKAGE_ARCHITECTURE ${ARCH_TEXT_PACKAGE})
    SET(CPACK_RPM_PACKAGE_VERSION ${PROJECT_VERSION_SHORT})
    SET(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/postinst")
    SET(CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/prerm")
    SET(CPACK_RPM_CHANGELOG_FILE ${PROJECT_CHANGELOG_FILE})
    #DEB
    IF(ARCH_TEXT_PACKAGE STREQUAL x86_64)
      SET(ARCH_TEXT_PACKAGE amd64)
    ENDIF()
    # SET(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
    # CPACK_DEBIAN_PACKAGE_HOMEPAGE The URL of the web site for this package
    # SET(CPACK_DEBIAN_PACKAGE_DEBUG ON)
    SET(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6 (>= 2.12), libstdc++6 (>= 4.4.7)")
    SET(CPACK_DEBIAN_PACKAGE_PRIORITY "optional")
    SET(CPACK_DEBIAN_PACKAGE_SECTION "database")  #input our section
    SET(CPACK_DEBIAN_PACKAGE_RECOMMENDS "Unknown")
    SET(CPACK_DEBIAN_PACKAGE_SUGGESTS "Unknown")
    SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE ${ARCH_TEXT_PACKAGE})#i386
    SET(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/postinst;${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/prerm")
    #GENTOO
    CONFIGURE_FILE("${CMAKE_SOURCE_DIR}/install/gentoo.ebuild.in" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${CPACK_PACKAGE_FILE_NAME}.ebuild" @ONLY IMMEDIATE)
  ELSEIF(OS_ANDROID)
    FIND_HOST_PROGRAM(ANDROID_DEPLOY_QT NAMES androiddeployqt PATHS ${QT_BINS_DIR})

    # Look for androiddeployqt program
    IF(NOT ANDROID_DEPLOY_QT)
        MESSAGE(FATAL_ERROR "Could not find androiddeployqt in ${QT_BINS_DIR} .")
    ENDIF(NOT ANDROID_DEPLOY_QT)

    # Set version
    SET(ANDROID_VERSION_NAME ${PROJECT_VERSION})
    SET(ANDROID_KEYSTORE_ALIAS ${PROJECT_COMPANYNAME})

    # set android package source for androiddeployqt json file
    SET(PACKAGE_SOURCE_ANDROID ${CMAKE_CURRENT_BINARY_DIR}/android)
    SET(PACKAGE_OUTPUT_ANDROID ${CMAKE_INSTALL_PREFIX})
    CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/android/strings.xml.in
      ${PACKAGE_SOURCE_ANDROID}/strings.xml @ONLY)
    CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/android/AndroidManifest.xml.in
      ${PACKAGE_SOURCE_ANDROID}/AndroidManifest.xml @ONLY)

    # create json file parsed by the androiddeployqt
    SET(ANDROID_SDK $ENV{ANDROID_SDK})

    SET(ANDROID_TARGET_ARCH ${ANDROID_NDK_ABI_NAME})
    #SET(ANDROID_TARGET_ARCH $ENV{ANDROID_TARGET_ARCH})
    SET(ANDROID_BUILD_TOOLS_REVISION $ENV{ANDROID_BUILD_TOOLS_REVISION})
    CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/android/configAndroid.json.in
      ${PACKAGE_SOURCE_ANDROID}/configAndroid.json @ONLY)

    SET(ANDROID_PACKAGE_RELEASE_NAME ${CPACK_PACKAGE_FILE_NAME}.apk)

    ADD_CUSTOM_COMMAND (
      OUTPUT apk_release_command
      DEPENDS ${PACKAGE_SOURCE_ANDROID}/AndroidManifest.xml
      COMMAND ${CMAKE_COMMAND} -E copy_directory
              ${CMAKE_SOURCE_DIR}/install/${PROJECT_NAME_LOWERCASE}/android/res
              ${PACKAGE_SOURCE_ANDROID}/res
      COMMAND ${ANDROID_DEPLOY_QT} --gradle --release
              --output ${PACKAGE_OUTPUT_ANDROID}
              --input ${PACKAGE_SOURCE_ANDROID}/configAndroid.json
      COMMAND ${CMAKE_COMMAND} -E rename
              ${PACKAGE_OUTPUT_ANDROID}/build/outputs/apk/install-release-unsigned.apk
              ${PACKAGE_OUTPUT_ANDROID}/${ANDROID_PACKAGE_RELEASE_NAME}
    )

    SET(ANDROID_PACKAGE_RELEASE_ALIGNED_NAME ${CPACK_PACKAGE_FILE_NAME}_aligned.apk)
    ADD_CUSTOM_COMMAND (
      OUTPUT apk_aligned_command
      DEPENDS ${PACKAGE_OUTPUT_ANDROID}/${ANDROID_PACKAGE_RELEASE_NAME}
      COMMAND ${ANDROID_SDK}/build-tools/${ANDROID_BUILD_TOOLS_REVISION}/zipalign
              -v 4 ${PACKAGE_OUTPUT_ANDROID}/${ANDROID_PACKAGE_RELEASE_NAME}
              ${PACKAGE_OUTPUT_ANDROID}/${ANDROID_PACKAGE_RELEASE_ALIGNED_NAME}
    )

    SET(ANDROID_PACKAGE_RELEASE_SIGNED_NAME ${CPACK_PACKAGE_FILE_NAME}_signed.apk)
    ADD_CUSTOM_COMMAND (
      OUTPUT apk_signed_command
      DEPENDS ${PACKAGE_OUTPUT_ANDROID}/${ANDROID_PACKAGE_RELEASE_ALIGNED_NAME}
      COMMAND apksigner sign
              --ks ~/$ENV{ANDROID_KEYSTORE}
              --ks-key-alias ${ANDROID_KEYSTORE_ALIAS}
              --ks-pass pass:$ENV{ANDROID_KEYSTORE_PASSWD}
              --key-pass pass:$ENV{ANDROID_KEY_PASSWD}
              --out ${PACKAGE_OUTPUT_ANDROID}/${ANDROID_PACKAGE_RELEASE_SIGNED_NAME}
              ${PACKAGE_OUTPUT_ANDROID}/${ANDROID_PACKAGE_RELEASE_ALIGNED_NAME}
    )

    ADD_CUSTOM_COMMAND (
      OUTPUT apk_install_to_device_command
      DEPENDS ${PACKAGE_OUTPUT_ANDROID}/${ANDROID_PACKAGE_RELEASE_SIGNED_NAME}
      COMMAND ${ANDROID_SDK}/platform-tools/adb install -r
              ${PACKAGE_OUTPUT_ANDROID}/${ANDROID_PACKAGE_RELEASE_SIGNED_NAME}
    )

    # Command to create apk from Makefile
    ADD_CUSTOM_TARGET(apk_release
      DEPENDS apk_release_command
    )

    # Command to create aligned apk from Makefile
    ADD_CUSTOM_TARGET(apk_aligned
      DEPENDS apk_aligned_command
    )

    # Command to create signed apk from Makefile
    ADD_CUSTOM_TARGET(apk_signed
      DEPENDS apk_signed_command
    )

    # Command to install the aligned signed apk through adb from Makefile
    ADD_CUSTOM_TARGET(apk_install_to_device
      DEPENDS apk_install_to_device_command
    )
  ENDIF(OS_WINDOWS)
  INCLUDE(CPack)
ENDIF(CPACK_SUPPORT)

#DOCS
IF(DEVELOPER_GENERATE_DOCS)
  CREATE_DOCS(${PROJECT_NAME_LOWERCASE} ${CMAKE_SOURCE_DIR}/docs/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
ENDIF(DEVELOPER_GENERATE_DOCS)

IF(NOT EXISTS ${CMAKE_SOURCE_DIR}/.git/hooks/pre-commit)
  EXECUTE_PROCESS(COMMAND ${CMAKE_SOURCE_DIR}/scripts/create_pre_commit_hook.sh)
ENDIF(NOT EXISTS ${CMAKE_SOURCE_DIR}/.git/hooks/pre-commit)
